package com.cction.activity.reinterest

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.cction.R
import com.cction.comm.CctionPresenterImpl
import com.cction.comm.MD5Utils
import com.cction.comm.Utils
import com.cction.entry.CctionBean
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.cction_view.*
import kotlinx.android.synthetic.main.cction_view.ction_calculate
import kotlinx.android.synthetic.main.title_bar.*
import kotlin.math.ceil


/**
 * 复利计算
 */
class CctionNewActivity : AppCompatActivity() {
    private var context = this
    private val cctionPresenter = CctionPresenterImpl()
    private var cctionBean = CctionBean()
    private var type: Int = 1
    private var ways = emptyArray<String>()
    private var adapter: ArrayAdapter<String>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.cction_view)
        initView()
    }

    private fun initView() {
        title_bar_back.visibility = View.VISIBLE
        title_bar_back.setOnClickListener { finish() }

        title_bar_title.text = "复利计算"
        title_bar_other.text = "清空"

        ways = resources.getStringArray(R.array.fl_ways)
        adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ways)
        cction_ways.adapter = adapter
        adapter?.notifyDataSetChanged()


        title_bar_other.setOnClickListener { clear() }
        cction_detail.setOnClickListener { toDetail() }
        cction_fl.setOnClickListener { toggleTab(1) }
        cction_bj.setOnClickListener { toggleTab(2) }
        cction_dt.setOnClickListener { toggleTab(3) }
        cction_dt_bj.setOnClickListener { toggleTab(4) }
        ction_calculate.setOnClickListener { calculate() }
    }

    ///清理
    private fun clear() {
        cction_money_value.text = "*****"
        cction_totle_value.text = "*****"
        cction_investment_money_value.text.clear()
        cction_investment_date_value.text.clear()
        cction_rate_edit.text.clear()
        cction_detail.visibility = View.GONE
    }

    //切换tab
    private fun toggleTab(type: Int) {
        this.type = type
        cction_fl.setTextColor(resources.getColor(R.color.colorPrimary))
        cction_bj.setTextColor(resources.getColor(R.color.colorPrimary))
        cction_dt.setTextColor(resources.getColor(R.color.colorPrimary))
        cction_dt_bj.setTextColor(resources.getColor(R.color.colorPrimary))
        cction_fl.setBackgroundColor(resources.getColor(R.color.white))
        cction_bj.setBackgroundColor(resources.getColor(R.color.white))
        cction_dt.setBackgroundColor(resources.getColor(R.color.white))
        cction_dt_bj.setBackgroundColor(resources.getColor(R.color.white))
        title_bar_other.visibility = View.GONE
        when (type) {
            1 -> {
                cction_title.text = cction_fl.text
                cction_sub_title.text = "计算收益"
                cction_investment_money_str.text = "复利现值"
                cction_money_str.text = "复利本金"
                cction_totle_str.text = "复利终值"
                cction_investment_ways.text = "复利方式"
                ways = resources.getStringArray(R.array.fl_ways)
                adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ways)
                cction_ways.adapter = adapter
                adapter?.notifyDataSetChanged()
                cction_fl.setTextColor(resources.getColor(R.color.white))
                cction_fl.setBackgroundColor(resources.getColor(R.color.colorPrimary))
            }
            2 -> {
                cction_title.text = cction_bj.text
                cction_sub_title.text = "反算本金"
                cction_investment_money_str.text = "复利终值"
                cction_money_str.text = "复利本金"
                cction_totle_str.text = "复利终值"
                cction_investment_ways.text = "复利方式"
                ways = resources.getStringArray(R.array.fl_ways)
                adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ways)
                cction_ways.adapter = adapter
                adapter?.notifyDataSetChanged()
                cction_bj.setTextColor(resources.getColor(R.color.white))
                cction_bj.setBackgroundColor(resources.getColor(R.color.colorPrimary))
            }
            3 -> {
                cction_title.text = cction_dt.text
                cction_sub_title.text = "计算收益"
                cction_investment_money_str.text = "定投现值"
                cction_money_str.text = "每次定投"
                cction_totle_str.text = "定投终值"
                cction_investment_ways.text = "定投方式"
                ways = resources.getStringArray(R.array.dt_ways)
                adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ways)
                cction_ways.adapter = adapter
                adapter?.notifyDataSetChanged()
                cction_dt.setTextColor(resources.getColor(R.color.white))
                cction_dt.setBackgroundColor(resources.getColor(R.color.colorPrimary))
            }
            4 -> {
                cction_title.text = cction_dt_bj.text
                cction_sub_title.text = "反算定投本金"
                cction_investment_money_str.text = "定投终值"
                cction_money_str.text = "每次定投"
                cction_totle_str.text = "定投终值"
                cction_investment_ways.text = "定投方式"
                ways = resources.getStringArray(R.array.dt_ways)
                adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, ways)
                cction_ways.adapter = adapter
                adapter?.notifyDataSetChanged()
                cction_dt_bj.setTextColor(resources.getColor(R.color.white))
                cction_dt_bj.setBackgroundColor(resources.getColor(R.color.colorPrimary))
            }
        }

    }

    ///输入校对
    private fun checkData(): Boolean {
        when (type) {
            1 -> {
                val cctionInvestmentMoneyValue = cction_investment_money_value.text
                if (cctionInvestmentMoneyValue == null || cctionInvestmentMoneyValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入复利现值", Toast.LENGTH_SHORT).show()
                    return false
                }

                val cctionInvestmentDateValue = cction_investment_date_value.text
                if (cctionInvestmentDateValue == null || cctionInvestmentDateValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入投资期限", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionRateEdit = cction_rate_edit.text
                if (cctionRateEdit == null || cctionRateEdit.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入利率", Toast.LENGTH_SHORT).show()
                    return false
                }
            }
            2 -> {
                val cctionInvestmentMoneyValue = cction_investment_money_value.text
                if (cctionInvestmentMoneyValue == null || cctionInvestmentMoneyValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入复利终值", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionInvestmentDateValue = cction_investment_date_value.text
                if (cctionInvestmentDateValue == null || cctionInvestmentDateValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入投资期限", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionRateEdit = cction_rate_edit.text
                if (cctionRateEdit == null || cctionRateEdit.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入利率", Toast.LENGTH_SHORT).show()
                    return false
                }
            }
            3 -> {
                val cctionInvestmentMoneyValue = cction_investment_money_value.text
                if (cctionInvestmentMoneyValue == null || cctionInvestmentMoneyValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入定投现值", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionInvestmentDateValue = cction_investment_date_value.text
                if (cctionInvestmentDateValue == null || cctionInvestmentDateValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入投资期限", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionRateEdit = cction_rate_edit.text
                if (cctionRateEdit == null || cctionRateEdit.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入利率", Toast.LENGTH_SHORT).show()
                    return false
                }
            }
            4 -> {
                val cctionInvestmentMoneyValue = cction_investment_money_value.text
                if (cctionInvestmentMoneyValue == null || cctionInvestmentMoneyValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入定投终值", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionInvestmentDateValue = cction_investment_date_value.text
                if (cctionInvestmentDateValue == null || cctionInvestmentDateValue.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入投资期限", Toast.LENGTH_SHORT).show()
                    return false
                }
                val cctionRateEdit = cction_rate_edit.text
                if (cctionRateEdit == null || cctionRateEdit.toString().trim().isEmpty()) {
                    Toast.makeText(context, "请输入利率", Toast.LENGTH_SHORT).show()
                    return false
                }
            }
        }
        return true
    }

    ///计算
    private fun calculate() {
        if (!checkData()) return
        var result = ""
        cctionBean = CctionBean()
        val money = cction_investment_money_value.text.toString()


        val waysSpnner = cction_ways.selectedItem.toString()
        val date = cction_investment_date_value.text.toString()
        val rate = cction_rate_edit.text.toString()

        var currentRate: String = ""
        val rateSpinner = cction_rate.selectedItem.toString()

        var currentDate: String = ""
        val cycleSpinner = cction_cycle.selectedItem.toString()
        when (waysSpnner) {
            "按年复利" -> {
                cctionBean.operation_type = 1
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = rate
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = (rate.toDouble() * 12).toString()
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = (rate.toDouble() * 365).toString()
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = date
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = ceil(date.toInt() / 12.0).toString()
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = ceil(date.toInt() / 365.0).toString()
                    }
                }


            }
            "按月复利" -> {
                cctionBean.operation_type = 2
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = (rate.toDouble() / 12).toString()
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = rate
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = (rate.toDouble() * 30).toString()
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = (date.toInt() * 12).toString()
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = date
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = ceil(date.toInt() / 30.0).toString()
                    }
                }
            }
            "按日复利" -> {
                cctionBean.operation_type = 3
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = (rate.toDouble() / 365).toString()
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = (rate.toDouble() / 30).toString()
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = rate
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = (date.toInt() * 365).toString()
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = (date.toInt() * 30.0).toString()
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = date
                    }
                }
            }
            "按年定投" -> {
                cctionBean.operation_type = 1
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = rate
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = (rate.toDouble() * 12).toString()
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = (rate.toDouble() * 365).toString()
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = date
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = (date.toInt() * 12.0).toString()
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = (date.toInt() * 365).toString()
                    }
                }
            }
            "按月定投" -> {
                cctionBean.operation_type = 2
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = (rate.toDouble() / 12).toString()
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = rate
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = (rate.toDouble() * 30).toString()
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = (date.toInt() * 12).toString()
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = date
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = (date.toInt() / 30).toString()
                    }
                }
            }
            "按日定投" -> {
                cctionBean.operation_type = 3
                when (rateSpinner) {
                    "年利率" -> {
                        cctionBean.interest_type = 1
                        currentRate = (rate.toDouble() / 365).toString()
                    }
                    "月利率" -> {
                        cctionBean.interest_type = 2
                        currentRate = (rate.toDouble() / 30).toString()
                    }
                    "日利率" -> {
                        cctionBean.interest_type = 3
                        currentRate = rate
                    }
                }

                when (cycleSpinner) {
                    "年" -> {
                        cctionBean.date_type = 1
                        currentDate = (date.toInt() * 365).toString()
                    }
                    "月" -> {
                        cctionBean.date_type = 2
                        currentDate = (date.toInt() * 30.0).toString()
                    }
                    "日" -> {
                        cctionBean.date_type = 3
                        currentDate = date
                    }
                }
            }
        }
        cctionBean.id = MD5Utils.getMd5Value("${System.currentTimeMillis()}")
        cctionBean.type = type
        cctionBean.date = currentDate
        cctionBean.interest = currentRate
        cctionBean.ctime = Utils.formatDate(System.currentTimeMillis(), "yyyy-MM-dd")
        when (type) {
            1 -> {
                result = cctionPresenter.ctionCompound(money, currentRate, currentDate)
                cctionBean.principal = money
                cctionBean.result = result
                cction_money_value.text = money
                cction_totle_value.text = result
                cction_detail.visibility = View.VISIBLE
            }
            2 -> {
                result = cctionPresenter.ctionPrincipal(money, currentRate, currentDate)
                cctionBean.principal = result
                cctionBean.result = money
                cction_money_value.text = result
                cction_totle_value.text = money
                cction_detail.visibility = View.VISIBLE
            }
            3 -> {
                result = cctionPresenter.ctionSurely(money, currentRate, currentDate)
                cctionBean.principal = money
                cctionBean.result = result
                cction_money_value.text = money
                cction_totle_value.text = result
                cction_detail.visibility = View.VISIBLE
            }
            4 -> {
                result = cctionPresenter.ctionSurelyPrincipal(money, currentRate, currentDate)
                cctionBean.principal = money
                cctionBean.result = result
                cction_money_value.text = result
                cction_totle_value.text = money
                cction_detail.visibility = View.VISIBLE
            }
        }
        title_bar_other.visibility = View.VISIBLE
    }

    ///去详情页
    private fun toDetail() {
        val intent = Intent()
        intent.setClass(context, CCtionDetailActivity::class.java)
        val bundle = Bundle()
        bundle.putSerializable("time", cctionBean)
        intent.putExtras(bundle)
        startActivity(intent)
    }


    public override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

}